import ElementPlus from 'element-plus';
import { createPinia } from 'pinia';
import { getMenu } from './service/apis';

export function onRouterCreated({ router }: any) {
  // console.log('onRouterCreated', router);
}

// 在此处写相当于常规vue项目的一些配置
export async function onAppCreated({ app }: any) {
  // console.log('onAppCreated', app);
  const pinia = createPinia();
  app.use(ElementPlus);
  app.use(pinia);
}

// 此处可处理路由
export async function onMounted({ app, router }: any) {
  // console.log('onMounted', app, router);
  // 此处写路由拦截
  let menus = await getMenu({});
  console.log(menus.data);
  let routes = [];
  menus.data.forEach((item) => {
    let route: any = {
      path: item.path,
      meta: { title: item.menuName },
      component: item.component,
    };
    if (item.children && item.children.length > 0) {
      let childRoutes = [];
      item.children.forEach((items) => {
        childRoutes.push({
          path: items.path,
          meta: { title: items.menuName },
          component: items.component,
        });
      });
      route.routes = childRoutes;
    }
    routes.push(route);
  });
  console.log(routes);
  // let routes = generateRoute(menus.data);
  // console.log(routes);
  // console.log(app);
  // router
  console.log(router);
  routes.map((route) => {
    router.addRoute(route);
  });
  console.log(router);
  router.beforeEach((to: any, from: any, next: Function) => {
    // console.log('router beforeEach', to, from);
    // console.log(router);
    document.title = to.meta.title ?? '系统';
    next();
    // document.title = to
  });
  // or
  // router.beforeEach((to: any, from: any) => {
  //   console.log('router beforeEach', to, from);
  // });
}

export const router = {
  // linkExactActiveClass: 'is-active',
  // @ts-ignore
  scrollBehavior(to, from) {
    // console.log('scrollBehavior', to, from);
  },
};
